package com.jyw.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jyw.R;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.EventBusTypes;
import com.jyw.pojo.UserInfo;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/8
 */

public class RecommondStatisticActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv0, tv1, tv2, tv3;
    private RelativeLayout layoutIntegral, layoutBalance;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommond_statistic);

        tv0 = (TextView) findViewById(R.id.tv_0);
        tv1 = (TextView) findViewById(R.id.tv_1);
        tv2 = (TextView) findViewById(R.id.tv_2);
        tv3 = (TextView) findViewById(R.id.tv_3);
        layoutBalance = (RelativeLayout) findViewById(R.id.layout_balance);
        layoutIntegral = (RelativeLayout) findViewById(R.id.layout_integral);
        layoutBalance.setOnClickListener(this);
        layoutIntegral.setOnClickListener(this);
        findViewById(R.id.iv_back).setOnClickListener(this);

        loadData();
    }

    private void loadData() {
        final Request request = new Request.Builder().
                url(Server.GET_USER_INFO + "?phone_data=" + getPrefs().getString("phone", "")
                ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(RecommondStatisticActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final UserInfo userInfo = new Gson().fromJson(response.body().string(), UserInfo.class);
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        tv0.setText(userInfo.getH_u_recommended_count());
                        tv1.setText(userInfo.getH_u_recommended_convertible_num());
                        tv2.setText(userInfo.getConvertible_integral());
                        tv3.setText(userInfo.getConvertible_balance());
                    }
                }.execute();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_balance:
                goBalance();
                break;
            case R.id.layout_integral:
                goIntegral();
                break;
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }

    private void goBalance() {
        final Request request = new Request.Builder().
                url(Server.EXCHANGE_BALANCE
                ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(RecommondStatisticActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                EventBus.getDefault().post(new EventBusTypes.IntegralBalanceChaged());
                OkHttpManager.backgroundThreadShortToast(RecommondStatisticActivity.this, "" + response.body().string());
            }
        });
    }

    private void goIntegral() {
        final Request request = new Request.Builder().
                url(Server.EXCHANGE_INTEGRAL
                ).build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(RecommondStatisticActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                EventBus.getDefault().post(new EventBusTypes.IntegralBalanceChaged());
                OkHttpManager.backgroundThreadShortToast(RecommondStatisticActivity.this, "" + response.body().string());
            }
        });
    }
}
